<?php
/**
 * gcFramework
 *
 * Development framework for PHP 5 or newer
 *
 * @package		gcFramework
 * @author		Adam Martin
 * @copyright	Copyright (c) 2006
 * @license		http://www.gcFramework.com/web/license.html
 * @link		http://www.gcFramework.com
 * @since		Version 1.0
 */
 
 //instantiate class
 $form = new form;
 
 class form{
 
/*
|-----------------------------------------|
| BASIC FORM FUNCTIONS                    |
|-----------------------------------------|
*/

//$todo - auto validation (if name of field is email - then validate it is an email for example)

/*
| -------------------------------------------------------------------
|  form_open
| -------------------------------------------------------------------
| Use this to write an opening form tag. 
|
| Example Use:
|   form_open("name");
|	
*/
function open($name='', $action='', $method='post', $extras=array(), $id=''){
	if(!$name || $name=="" || $name==null){
		displayError(ERROR_FORM_NONAME);	
	}
	$form = '<form name="'.$name.'" method="'.$method.'" action="'.$action.'"';
	if (is_array($extras) AND count($extras) > 0){
		foreach ($extras as $key => $val){
			$form .= ' '.$key.'="'.$val.'"';  
		}
	}
	$form .= ">\n";
	return $form;
}

/*
| -------------------------------------------------------------------
|  form_multiopen
| -------------------------------------------------------------------
| Use this to write an opening form tag for use with file uploading. 
|
| Example Use:
|   form_multiopen("name");
|	
*/
function multiopen($name='', $method='post', $action='', $extras=array(), $id=''){
	if(!$name || $name=="" || $name==null){
		displayError(ERROR_FORM_NONAME);	
	}
	$extras['enctype']='multipart/form-data';
	$form = self::open($name, $method, $action, $extras);
	return $form;
}

/*
| -------------------------------------------------------------------
|  form_close
| -------------------------------------------------------------------
| Use this to write closing form tag. 
|
| Example Use:
|   form_close();
|	
*/
function close(){
	return "</form>\n";	
}

/*
| -------------------------------------------------------------------
|  form_input
| -------------------------------------------------------------------
| Use this to write an input field tag. 
|
| Example Use:
|   form_input("name", "value");
|   form_input("name", "value", "password");
|   form_input("name", "value", "hidden");
|   form_input("name", "value", "file");
|	
*/
function input($name='', $value='', $type='text', $extras=array(), $id=''){
	if(!$name || $name=="" || $name==null){
		displayError(ERROR_FORM_INPUT_NONAME);	
	}
	$form = ' <input type="'.$type.'" name="'.$name.'" id="'.$name.'" value="'.$value.'"';
	if (is_array($extras) AND count($extras) > 0){
		foreach ($extras as $key => $val){
			$form .= ' '.$key.'="'.$val.'"';  
		}
	}
	$form .= " />";
	return $form;
}

/*
| -------------------------------------------------------------------
|  form_checkbox @todo: allow multiple like radiobutton
| -------------------------------------------------------------------
| Use this to write a checkbox field tag. 
|
| Example Use:
|   form_checkbox("name", "value");
|   form_checkbox("name", "value", "checked");
|	
*/
function checkbox($name='', $value='', $checked=false, $extras=array(), $id=''){
	if(!$name || $name=="" || $name==null){
		displayError(ERROR_FORM_CHECKBOX_NONAME);	
	}
	if($checked != false){
		$checked="checked";
	} else {
		$checked="";
	}
	if(!$id || $id=="" || $id==null){
		$id=$name;	
	}
	$form = ' <input type="checkbox" name="'.$name.'" id="'.$id.'" value="'.$value.'"'.$checked;
	if (is_array($extras) AND count($extras) > 0){
		foreach ($extras as $key => $val){
			$form .= ' '.$key.'="'.$val.'"';  
		}
	}
	$form .= " />\n";
	return $form; 
}

/*
| -------------------------------------------------------------------
|  form_radio
| -------------------------------------------------------------------
| Use this to write radio button tags. 
|
| Example Use:
|   form_checkbox("name", "value");
|   form_checkbox("name", "value1, value2, value3", "value2");
|	
*/
function radio($name='', $values='', $checked=false, $extras=array(), $id=''){
	if(!$name || $name=="" || $name==null){
		displayError(ERROR_FORM_RADIO_NONAME);	
	}
	if(!strstr($values, ",")){
		$form = ' <input type="radio" name="'.$name.'" value="'.$values.'"';
		if (is_array($extras) AND count($extras) > 0){
			foreach ($extras as $key => $val){
				$form .= ' '.$key.'="'.$val.'"';  
			}
		}
		if($values == $checked){
			$form .= " checked ";
		}
		$form .= " />\n";
		return $form;
	} else {
		$values = explode(",", $values);
		foreach($values as $value){
			$form = ' <input type="radio" name="'.$name.'" value="'.$value.'"';
			if (is_array($extras) AND count($extras) > 0){
				foreach ($extras as $key => $val){
					$form .= ' '.$key.'="'.$val.'"';
					}  
				}
			if($value == $checked){
				$form .= " checked ";
			}
			$form .= " />\n";
			return $form;
		}
	}
}

/*
| -------------------------------------------------------------------
|  form_textarea
| -------------------------------------------------------------------
| Use this to write a textarea tag. 
|
| Example Use:
|   form_textarea("name", "value");
|	
*/
	function textarea($name='', $value='', $cols='50', $rows='10', $extras=array(), $id=''){
		if(!$name || $name=="" || $name==null){
		displayError(ERROR_FORM_TEXTAREA_NONAME);	
	}
		$form = ' <textarea name="'.$name.'" cols="'.$cols.'" rows="'.$rows.'"';
	if (is_array($extras) AND count($extras) > 0){
		foreach ($extras as $key => $val){
			$form .= ' '.$key.'="'.$val.'"';  
		}
	}
	$form .= " >".$value."</textarea>";
	//return $form;
	return $form;	
	}	

/*
| -------------------------------------------------------------------
|  form_opendropdown
| -------------------------------------------------------------------
| Use this to write an opening dropdown input tag. 
|
| Example Use:
|   form_opendropdown("name");
|   form_opendropdown("name", true);	<--allow multiple selections
|	
*/
function opendropdown($name='', $multiple=false, $id='', $selected = '', $extras = array(), $id=''){
	if(!$name || $name=="" || $name==null){
		displayError(ERROR_FORM_DROPDOWN_NONAME);	
	}
	if(!$id || $id==0 || $id==null){
		$id = $name;
	}
	if($multiple != false){
		$multiple=" multiple";
	} else {
		$multiple="";
	}
	$form = ' <select name="'.$name.'" id="'.$id.'"'.$multiple;
	if (is_array($extras) AND count($extras) > 0){
		foreach ($extras as $key => $val){
			$form .= ' '.$key.'="'.$val.'"';  
		}
	}
	$form .= " >\n";
	
	return $form;
}

/*
| -------------------------------------------------------------------
|  form_closedropdown
| -------------------------------------------------------------------
| Use this to write a closing dropdown tag. 
|
| Example Use:
|   form_closedropdown();
|	
*/
function closedropdown(){
	return " </select>\n";	
}

/*
| -------------------------------------------------------------------
|  form_options
| -------------------------------------------------------------------
| Use this to write options tags for a select list. 
|
| Example Use:
|   form_options(array('1'=>"test1", '2'=>"test2"));
|   form_options(array('1'=>"test1", '2'=>"test2"), "Group Name");
|	
*/
function options($options, $optgroup=false){
	if($optgroup){
		$form = '  <optgroup label="'.$optgroup.'">'."\n";
		foreach($options as $key => $val){
			$form .= '   <option value="'.$key.'">'.$val.'</option>'."\n";
		}
		$form .= "  </optgroup>\n";
	} else {
		foreach($options as $key => $val){
			$form .= '  <option value="'.$key.'">'.$val.'</option>';
		}
	}
	return $form;	
}

/*
| -------------------------------------------------------------------
|  form_label
| -------------------------------------------------------------------
| Defines a label to a control. If you click the text within the 
| label element,  it is supposed to toggle the control. 
|
| Example Use:
|   form_label(array("text", "control");
|   form_options(array('1'=>"test1", '2'=>"test2"), "Group Name");
|	
*/
function label($text, $control){
	$form = '<label for="'.$control.'">'.$text.'</label>';
	return $form;	
}

/*
| -------------------------------------------------------------------
|  form_fieldsetopen
| -------------------------------------------------------------------
| The fieldset element draws a box around its containing elements. 
| The legend element defines a caption for a fieldset.
|
| Example Use:
|   form_fieldsetopen($legend, $id);
|	
*/
function fieldsetopen($legend='', $id=''){
	$form = '<fieldset><legend>'.$legend.'</legend>';
	return $form;	
}

/*
| -------------------------------------------------------------------
|  form_fieldsetopen
| -------------------------------------------------------------------
| Close fieldset
|
| Example Use:
|   form_fieldsetclose();
|	
*/
function fieldsetclose(){
	return '</fieldset>';	
}

/*
| -------------------------------------------------------------------
|  form_editor @todo: need to work on this to provide options
| -------------------------------------------------------------------
| Enable a wysiwyg editor
|
| Example Use:
|   form_editor();
|	
*/
function editor(){
	$editor = '<object type="application/x-xstandard" id="editor1" width="100%" height="400">'."\n";
	$editor .= '<param name="Value" value="Hello World!" />'."\n";
	$editor .= '</object>'."\n";	
	return $editor;
}

/*
|-----------------------------------------|
| ADVANCED FORM FUNCTIONS                 |
|-----------------------------------------|
*/

//@todo: this won't work with correctly - must return (not echo all elements)

function contact($name='', $email='') {
	global $gcvalidate;
	$gcvalidate->email($email);
	$form = self::open("contact");
	$form .= self::input("name");
	$form .= self::input("email");
	$form .= self::textarea("message");
	$form .= self::input("submit", "send", "submit");
	$form .= self::input("reset", "clear", "reset");
	return $form;
}

/*
| -------------------------------------------------------------------
|  form_countrylist
| -------------------------------------------------------------------
| Build a dropdown country list
|
| Example Use:
|   form_countrylist("Please choose your country", "AU");
|	
*/
function countrylist($selected='', $firstline=''){
	global $gcyaml;
	$countries = $gcyaml->load('countries');
	$form = '<select name="country" id="country" >'."\n";
	if($firstline !='') {
		$form .= ' <option>'.$firstline.'</option>'."\n";
	}
	foreach($countries as $key=>$val){
		if($val["code"] == $selected){
			$form .= ' <option value="'.$val["code"].'" selected="selected">'.$val["name"].'</option>'."\n";
		} else {
			$form .= ' <option value="'.$val["code"].'">'.$val["name"].'</option>'."\n";
		}	
	}
	$form .= '</select>'."\n";
	return $form;
}
 }
?>
